package com.diyunapp.xilinapp.evaluations;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.selfdefine.FragmentAdapter;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by NIDAYE on 2016/6/21.
 */
public class EvaluationActivity extends StatusBarFragmentActivity implements ViewPager.OnPageChangeListener {

    @Bind(R.id.evaluation_goback_ib)
    ImageButton evaluationGobackIb;
    @Bind(R.id.evaluation_total_rb)
    RadioButton evaluationTotalRb;
    @Bind(R.id.evaluation_good_rb)
    RadioButton evaluationGoodRb;
    @Bind(R.id.evaluation_middle_rb)
    RadioButton evaluationMiddleRb;
    @Bind(R.id.evaluation_bad_rb)
    RadioButton evaluationBadRb;
    @Bind(R.id.evaluation_select_rg)
    RadioGroup evaluationSelectRg;
    @Bind(R.id.goods_evaluation_vp)
    ViewPager goodsEvaluationVp;

    private View headView;

    private List<Fragment> myEvaluationList;
    private FragmentAdapter myEvaluationPagerAp;

    private Fragment totalEvaluationFrag;
    private Fragment goodEvaluationFrag;
    private Fragment middleEvaluatioFrag;
    private Fragment badEvaluationFrag;

    public static String good_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goods_userevaluation_layout);
        ButterKnife.bind(this);

        good_id = getIntent().getStringExtra("GoodId");

        initViews();
        getEvaluationNum();
    }

    private void initViews() {

        headView = findViewById(R.id.evaluation_menubar_inc);
        super.MenuWidget(headView);

        evaluationGobackIb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        myEvaluationList = new ArrayList<>();
        totalEvaluationFrag = new AllEvaluationFragment();
        goodEvaluationFrag = new GoodEvaluationFragment();
        middleEvaluatioFrag = new MiddleEvaluationFragment();
        badEvaluationFrag = new BadEvaluationFragment();

        myEvaluationList.add(totalEvaluationFrag);
        myEvaluationList.add(goodEvaluationFrag);
        myEvaluationList.add(middleEvaluatioFrag);
        myEvaluationList.add(badEvaluationFrag);

        myEvaluationPagerAp = new FragmentAdapter(this.getSupportFragmentManager(), myEvaluationList);
        goodsEvaluationVp.setAdapter(myEvaluationPagerAp);
        goodsEvaluationVp.setOnPageChangeListener(this);
        myEvaluationPagerAp.notifyDataSetChanged();

        evaluationSelectRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.evaluation_total_rb:
                        goodsEvaluationVp.setCurrentItem(0);
                        break;
                    case R.id.evaluation_good_rb:
                        goodsEvaluationVp.setCurrentItem(1);
                        break;
                    case R.id.evaluation_middle_rb:
                        goodsEvaluationVp.setCurrentItem(2);
                        break;
                    case R.id.evaluation_bad_rb:
                        goodsEvaluationVp.setCurrentItem(3);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    private void clickRadioButton(int state) {
        switch (state) {
            case 0:
                evaluationTotalRb.setChecked(true);
                break;
            case 1:
                evaluationGoodRb.setChecked(true);
                break;
            case 2:
                evaluationMiddleRb.setChecked(true);
                break;
            case 3:
                evaluationBadRb.setChecked(true);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Log.i("zhangxiaoxidebug", "evaluation position=" + position);
        clickRadioButton(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void getEvaluationNum() {
        String url = "http://www.xilingou.com/mobile/index.php?act=goods&op=goods_evaluate&goods_id=" + good_id;
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(200);
        http.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        int result = parseProject(responseInfo.result);
                        switch (result) {
                            case 1:
                                clickRadioButton(0);
                                break;
                            case 0:
                                Toast.makeText(getApplicationContext(), "查询失败！", Toast.LENGTH_LONG).show();
                                break;
                            default:
                                break;
                        }
                    }


                    @Override
                    public void onStart() {

                    }

                    private int parseProject(String result) {
                        Log.i("zhangxiaoxidebug", "detail_result=" + result);
                        try {
                            JSONObject jsObjectCode = new JSONObject(result);
                            int code = jsObjectCode.getInt("code");
                            if (code != 200) {
                                return 0;
                            }

                            JSONObject jsObjectDate = jsObjectCode.getJSONObject("date");
                            String total = jsObjectDate.getString("all");
                            String cha = jsObjectDate.getString("cha");
                            String zhong = jsObjectDate.getString("zhong");
                            String hao = jsObjectDate.getString("hao");

                            evaluationTotalRb.setText("全部(" + total + ")");
                            evaluationGoodRb.setText("好评(" + hao + ")");
                            evaluationMiddleRb.setText("中评(" + zhong + ")");
                            evaluationBadRb.setText("差评(" + cha + ")");
                        } catch (JSONException e) {
                            e.printStackTrace();
                            return 0;
                        }
                        return 1;
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        Log.i("zhangxiaoxidebug", "getIndex....onFailure msg=" + msg);
                        Toast.makeText(getApplicationContext(), "读取数据失败，请检查网络设置！", Toast.LENGTH_LONG).show();
                    }
                }
        );
    }

}
